package com.example.springbootdemo.utils;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.rendering.PDFRenderer;

/**
 * @ClassName: PdfUtils
 * @Description:
 * @Author: TXW
 * @Date: 2023-07-14
 */

public class PdfUtils {

    //给pdf添加图片
    public static void addImageToPDF(String pdfFilePath, String imageFilePath, float x, float y, float width, float height) throws IOException {
        PDDocument document = null;
        try {
            document = PDDocument.load(new File(pdfFilePath));
            PDPage page = document.getPage(0); // 获取第一页

            PDImageXObject image = PDImageXObject.createFromFile(imageFilePath, document);

            PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
            contentStream.drawImage(image, x, y, width, height);
            contentStream.close();

            document.save(pdfFilePath);
        } finally {
            if (document != null) {
                document.close();
            }
        }
    }
    //获取pdf某一点的位置，初始位置为左上角
    public static Rectangle2D getPointLocation(String filePath, int pageIndex, float x, float y) throws IOException {
        PDDocument document = null;
        try {
            document = PDDocument.load(new File(filePath));
            PDPage page = document.getPage(pageIndex);

            PDFRenderer renderer = new PDFRenderer(document);
            BufferedImage image = renderer.renderImage(pageIndex);

            int imageWidth = image.getWidth();
            int imageHeight = image.getHeight();

            float pageWidth = page.getMediaBox().getWidth();
            float pageHeight = page.getMediaBox().getHeight();

            float scale = Math.min(imageWidth / pageWidth, imageHeight / pageHeight);

            float scaledX = x * scale;
            float scaledY = y * scale;

            Rectangle2D.Float rect = new Rectangle2D.Float(scaledX, scaledY, 1, 1);

            return rect;
        } finally {
            if (document != null) {
                document.close();
            }
        }
    }


    public static String getPdfWidthHeight(String filePath, int pageIndex) throws IOException {
        PDDocument document = null;
        try {
            document = PDDocument.load(new File(filePath));
            PDPage page = document.getPage(pageIndex);

            float pageWidth = page.getMediaBox().getWidth();
            float pageHeight = page.getMediaBox().getHeight();

            return "PDF宽度为："+pageWidth+"高度为："+pageHeight;
        } finally {
            if (document != null) {
                document.close();
            }
        }
    }

}
